<?php

	/**
	* @author Robbie Vasquez
 	* @author Matt Kennedy
	*
	* @param username
 	* @param password
 	* @param email
 	*/
	
class Register extends Model
{
	//creating variables
	var $username = '';
	var $password = '';
	var $email = '';
	var $pw_rec_question = '';
	var $pw_rec_answer = '';
	
		
	public function Register()
	{
		// Call the model constructor
		parent::Model();
		
		$this->load->database();
	}
	
	public function process()
	{		
		if(isset($_POST) && $_POST != NULL)
		{
			$errors = array();
			
			if(trim($_POST['username']) == '')
				$errors[] = "You must enter a username.";
			
			if(trim($_POST['password']) == '')
				$errors[] = "You must enter a password.";
				
			if(trim($_POST['email']) == '')
				$errors[] = "You must enter an email.";
			
			if(trim($_POST['pw_rec_answer']) == '')
				$errors[] = "You must choose and answer a security question.";
			
			if(count($errors) < 1)
			{
				$data = array('username' => $_POST['username'], 'password' => $_POST['password'], 'email' => $_POST['email'], 'pw_rec_question' => $_POST['pw_rec_question'], 'pw_rec_answer' => $_POST['pw_rec_answer']);

				mysql_connect('2006.ispace.ci.fsu.edu', 'rmv06d', 'zqgvht2m');
				mysql_select_db('runningrecords');
				
				$sql = "SELECT * FROM users WHERE username='" . $data['username'] . "' OR email='" . $data['email'] . "'";
				$result = mysql_query($sql);
				$pull = mysql_fetch_array($result);
				
				if($pull === FALSE)
				{
					$this->db->insert('users', $data);
					
					session_start();
					$_SESSION['auth'] = 'yes';
					$_SESSION['username'] = $data['username'];
					
					header("Location: /runningrecords/index.php/main/home");				
					die();
				}
				
				else
					$errors[] = "That account has already registered";
			}
			
			return $errors;
		}
	}
}


/* EOF */